<?php
/*
 * deposit_class.php
 * 
 * Copyright 2013  <>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

Class deposits  {
	
	protected $deposit_id;

	public function __construct($id) {
			   
		$this->deposit_id = $id;

	}
	
/*
 *
 * name: InsertDeposit
 *
 *              Insert a new deposit in the database
 *
 * @param:
 *              $planet_id: new deposit planet id
 *              $x_coord: x coord of the new deposit
 *              $y_coord: y coord of the new deposit
 *              $material: material of the new deposit
 *              $size: size of the new deposit
 *              $user: user name creating the deposit
 *
 * @return:
 *              true: in case of error (error msg)
 *              false: deposit inserted with no errors
 */
	public static function InsertDeposit($planet_id, $x_coord, $y_coord, $material, $size, $user){
		
		global $db;

		$query = "INSERT INTO deposits (planet_id, x_coord, y_coord, material, size, user) VALUES (?, ?, ?, ?, ?, ?)";
		
		try{
			$stmt = $db->prepare($query);
			
			$stmt->bindParam(1,$planet_id);
			$stmt->bindParam(2,$x_coord);
			$stmt->bindParam(3,$y_coord);
			$stmt->bindParam(4,$material);
			$stmt->bindParam(5,$size);
			$stmt->bindParam(6,$user);
			
			$stmt->execute();
			
		} catch(PDOException $e) {
			
			//if duplicate error
			if ($e->getCode() == '23000') {
				return "There is already a deposit in that place.";
			}
			return "Error!: " . $e->getMessage();

		}

		return false;
		   
	}
	
/*
 *
 * name: DepositDetail
 *
 *              Select all the data of a single deposit
 *
 * @param:
 *
 * @return:
 *              $row: associative array with the deposit data
 *              false: in case of error or no data
 */
	public function DepositDetail(){
		
		global $db;
			
		$query = "SELECT * FROM deposits WHERE id = ?";
			
		try {
			$stmt = $db->prepare($query);
			$stmt->bindParam(1,$this->deposit_id);
			$stmt->execute();
			
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			
		} catch(PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			return false;
		}

		//return $row if set
		if(isset($row)){
			   
				return $row;
			   
		} else {
				return false;
		}
               
	}

/*
 *
 * name: UpdateDeposit
 *
 *              Updates the data of a single deposit
 *
 * @param:
 *              $new_material: new material of the deposit
 *              $new_size: new size of the deposit
 *              $user_mod: user updating the deposit
 *
 * @return:
 *              true: in case of error
 *              false: no error, update successfull
 */
	public function UpdateDeposit($new_material, $new_size, $user_mod){
		   
		global $db;
					   
		$query  = "UPDATE deposits SET ";
		$query .= "material = ?, ";
		$query .= "size = ?, ";
		$query .= "user = ? ";
		$query .= "WHERE id = ? ";
		
		try {
			$stmt = $db->prepare($query);

			$stmt->bindParam(1,$new_material);
			$stmt->bindParam(2,$new_size);
			$stmt->bindParam(3,$user_mod);
			$stmt->bindParam(4,$this->deposit_id);
			
			$stmt->execute();
			
		} catch(PDOException $e) {
			return "Error!: " . $e->getMessage();
		}

		return false;
		   
	}

/*
 *
 * name: DeleteDeposit
 *
 *              Delete a single deposit of the database
 *
 * @param:
 *
 * @return:
 *              true: in case of error
 *              false: no error, delete successfull
 */
	public function DeleteDeposit(){
		   
		global $db;
	   
		$query  = "DELETE FROM deposits WHERE id = ?";
		
		try {
			$stmt = $db->prepare($query);
			$stmt->bindParam(1, $this->deposit_id);
			$stmt->execute();
			
		} catch(PDOException $e) {
			return "Error!: " . $e->getMessage();
		}

		return false;
	}
        
/*
 *
 * name: SelectAllDeposits
 *
 *              Select data of all the deposits in the db, ordering depending of input parameters
 *
 * @param:
 *              $order (optional): array with order rule/s of the form:
 *                      $order[] = $element; $element[0]="column" $element[1]=descending (TRUE or FALSE)
 *
 * @return:
 *              $data[]: array. Each row = associative array with deposit data
 *              false: error or no data
 */
	public static function SelectAllDeposits($order = FALSE){
		
		global $db;
		   
		$query = "SELECT d.id, p.name, d.x_coord, d.y_coord, d.material, d.size, d.user, d.time_mod ";
		$query .= "FROM deposits d, planets p ";
		$query .= "WHERE d.planet_id = p.id ";
	   
		//if $order is set, add the "order by" clause to the query
		if($order) {
			   
				$query .= "ORDER BY ";
			   
				//for each ordering rule (as $element)
				foreach($order as $element) {
					   
						//inspect $element[0] (order column)
						switch ($element[0]) {
								case "material":
										$query .= "d.material ";
										break;
								case "size":
										$query .= "d.size ";
										break;
								case "x_coord":
										$query .= "d.x_coord ";
										break;
								case "y_coord":
										$query .= "d.y_coord ";
										break;
								case "user":
										$query .= "d.user ";
										break;
								case "time_mod":
										$query .= "d.time_mod ";
										break;
						}
					   
						//if $element[1] is TRUE => the column is ordered in descending order
						if($element[1]) {
								$query .= "DESC,";
						} else {
								$query .= ",";
						}
			   
				}
				//remove the last comma in the query
				$query = rtrim($query, ",");
		}
		
		try {
			
			foreach ($db->query($query) as $row) {
				$data[] = $row;
			}
			
		} catch(PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			return false;
		}
	   
		//If there is data in the db, returns the result of the query. If not returns false
		if(isset($data)){
				return $data;
		} else {
				return false;
		}
		   
	}   
}
?>

